﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BSC;
using EmrDatastructure;
using MvcEmrRazor.Models;

namespace MvcEmrRazor.Controllers
{
    [Authorize]
    public class SmokingController : Controller
    {
        private IEmrService _service;

        public SmokingController()
        {
            _service = DALFactory.DataAccess.CreateDataAccessLayer();
        }
        public ActionResult Index()
        {
            EmrSmoking e = _service.ReadEmrSmoking(System.Web.HttpContext.Current.Session["DemographicID"].ToString());
            if (e == null)
            {
                return RedirectToAction("Create");
            }
            else
            {
                if (EmrUtil.EmrSmoking.ContainsKey(e.Status))
                {
                    ViewData["smoking_string"] = EmrUtil.EmrSmoking[e.Status];
                }
                else
                {
                    // need the api to delete an invalid value
                    ViewData["smoking_string"] = EmrUtil.EmrSmoking[0];
                }
                return View("Index", e);
            }
        }

        //
        // GET: /Smoking/Create
        public ActionResult Create()
        {
            ViewData["smoking_string"] = EmrUtil.EmrSmoking[0];
            ViewData["smoking"] = new SelectList(EmrUtil.EmrSmoking, "Key", "Value", 0);
            return View();
        }

        //
        // POST: /Smoking/Create
        [HttpPost]
        public ActionResult Create(EmrSmoking e, FormCollection collection)
        {
            try
            {
                e.Status = Convert.ToInt32(collection["smoking"]);
                if (e.Status == 0)
                {
                    ViewData["smoking"] = new SelectList(EmrUtil.EmrSmoking, "Key", "Value", 0);
                    ModelState.AddModelError(string.Empty, "You must select smoking status");
                    return View();
                }
                e.EmrDemographicId = System.Web.HttpContext.Current.Session["DemographicID"].ToString();
                _service.CreateEmrSmoking(e);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /Smoking/Edit/5
        public ActionResult Edit(int id)
        {
            EmrSmoking r = _service.ReadEmrSmoking(System.Web.HttpContext.Current.Session["DemographicID"].ToString());
            if (EmrUtil.EmrSmoking.ContainsKey(r.Status))
            {
                ViewData["smoking"] = new SelectList(EmrUtil.EmrSmoking, "Key", "Value", r.Status);
            }
            else
            {
                ViewData["smoking"] = new SelectList(EmrUtil.EmrSmoking, "Key", "Value", 0);
            }
            return View("Edit", r);
        }

        //
        // POST: /Smoking/Edit/5
        [HttpPost]
        public ActionResult Edit(EmrSmoking r, FormCollection collection)
        {
            try
            {
                r.Status = Convert.ToInt32(collection["smoking"]);
                if (r.Status == 0)
                {
                    ModelState.AddModelError(string.Empty, "You must select smoking status");
                    ViewData["smoking"] = new SelectList(EmrUtil.EmrSmoking, "Key", "Value", 0);
                    return View("Edit", r); 
                }
                _service.UpdateEmrSmoking(r);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Edit", r);
            }
        }
    }
}
